package com.easyCode.util;


import com.google.common.base.CaseFormat;

import java.io.File;
import java.util.List;
import java.util.stream.Collectors;


/**
 * @author 大忽悠
 * @create 2022/12/2 15:39
 */
public class StringUtil {

    /**
     * 下划线转驼峰: test_data ---> testData
     */
    public static  String underlineToCamel(String underlineStr){
        return CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, underlineStr);
    }

    /**
     * 驼峰转下划线: testData ---> test_data
     */
    public static  String camelToUnderline(String camelStr){
        return CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, camelStr);
    }

    /**
     * 首字母大写(进行字母的ascii编码前移，效率是最高的)
     *
     * @param str 需要转化的字符串
     */
    public static String firstCharToUpper(String str) {
        char[] chars = str.toCharArray();
        chars[0] = toUpperCase(chars[0]);
        return String.valueOf(chars);
    }

    /**
     * 首字母小写
     *
     * @param str 需要转化的字符串
     */
    public static String firstCharToLower(String str) {
        if(Character.isLowerCase(str.charAt(0))) {
            return str;
        }
        return Character.toLowerCase(str.charAt(0)) + str.substring(1);
    }

    /**
     * 字符转成大写
     *
     * @param c 需要转化的字符
     */
    public static char toUpperCase(char c) {
        if (97 <= c && c <= 122) {
            c ^= 32;
        }
        return c;
    }

    public static boolean isNullOrEmpty(String value) {
        if(value==null){
            return true;
        }
        return value.trim().isEmpty() || "".equals(value) || "\"\"".equals(value.trim());
    }

    /**
     * 清除目标字符串结尾的"/"字符
     */
    public static String cleanEndFileSeparator(String targetStr){
        return (targetStr.endsWith("/")||targetStr.endsWith("\\"))?targetStr.substring(0,targetStr.length()-1):targetStr;
    }

    public static String cleanStartFileSeparator(String targetStr) {
        return (targetStr.startsWith("/")||targetStr.startsWith("\\"))?targetStr.substring(1,targetStr.length()):targetStr;
    }

    public static String cleanStartAndEndFileSeparator(String targetStr){
        return cleanStartFileSeparator(cleanEndFileSeparator(targetStr));
    }

    public static String stripStrFromBegin(String stripStr,String targetStr){
        if(!targetStr.startsWith(stripStr)){
            return targetStr;
        }
        return targetStr.substring(stripStr.length());
    }

    public static String pathMerge(Boolean ignoreCleanFirstPathStartSeparator,String ... paths){
        StringBuilder stringBuilder=new StringBuilder();
        Integer index=1;
        for (String path : paths) {
            //忽略为空的路径
            if(StringUtil.isNullOrEmpty(path)){
                continue;
            }
            if(ignoreCleanFirstPathStartSeparator&&index.equals(1)){
                stringBuilder.append(cleanEndFileSeparator(path)).append(File.separator);
                index--;
                continue;
            }
            stringBuilder.append(cleanStartAndEndFileSeparator(path)).append(File.separator);
        }
        String path = cleanEndFileSeparator(stringBuilder.toString());
        return SystemUtil.isWindows()?path.replace('/','\\'):path.replace('\\','/');
    }

    public static List<String> camelToUnderline(List<String> columns) {
           return columns.stream().map(StringUtil::camelToUnderline).collect(Collectors.toList());
    }
}
